﻿namespace unTroublemaker
{
    class Program
    {
        static void Main(string[] inputArguments)
        {
            var arguments = new AppArguments(inputArguments);

            if (arguments.NotSpecified)
            {
                Output.Error("No input xml specification file specified");
                return;
            }

            var specification = new Specification(arguments.File);
            if (!specification.Valid)
            {
                Output.Error("Invalid specification: " + specification.Error);
                return;
            }

            var tester = new SpecificationTester(specification);
            tester.ItemStatusChanged += OutputCheckResults;
            var passed = tester.Run();
            
            if (!passed)
                Output.Error("One ore more checks in specification failed!");
            else
                Output.Success("All items in specification passed!");
        }

        static void OutputCheckResults(ISpecificationItem sender, StatusChangedEventArgs args)
        {
            if(args.NewRunStatus == RunStatus.Passed)
                Output.Success("Check passed " + sender.DisplayName);
            else if (args.NewRunStatus == RunStatus.Failed)
            {
                var statusMessage = string.IsNullOrEmpty(sender.StatusMessage)
                                        ? string.Empty
                                        : (": " + sender.StatusMessage);

                Output.Warning("Check failed " + sender.DisplayName + statusMessage);
            }
        }
    }
}
